package com.app.controller;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.app.entity.*;
import com.app.service.*;
import com.app.utils.MainUtils;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsService goodsService;

    @RequestMapping("/goods/add")
    public String goodsAdd() {
        return "admin/goods/add";
    }

    @RequestMapping(value = "/goods/save", method = RequestMethod.POST)
    public String goodsSave(String uid,
                            String code,
                            String product_name,
                            String description,
                            String thumb,
                            String content,
                            String category_name,
                            String click,
                            String category,
                            String store,
                            String vpath,
                            String price,
                            String created,
                            String updated,
                            String status,
                            Model model, HttpSession session) {
        uid = session.getAttribute("userid") + "";
        Goods modelX = new Goods();
        modelX.setUid(uid);
        modelX.setProduct_name(product_name);
        modelX.setDescription(description);
        modelX.setThumb(thumb);
        modelX.setContent(content);
        modelX.setPrice(price);
        modelX.setCreated(MainUtils.getTime());

        goodsService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "操作成功");
        return "admin/goods/add";
    }

    @RequestMapping("/goods/delete")
    public String goodsDelete(int id) {
        goodsService.delete(id);
        return "redirect:list";
    }

    @RequestMapping("/goods/list")
    public ModelAndView goodsList() {
        List<Goods> goodsList = goodsService.getAllGoods();
        System.out.println(goodsList.size());
        ModelAndView mav = new ModelAndView("admin/goods/list");
        mav.addObject("list", goodsList);
        return mav;
    }


    @RequestMapping("/goods/mylist")
    public ModelAndView mylist(HttpSession session) {
        String uid = session.getAttribute("userid") + "";
        List<Goods> goodsList = goodsService.getAllGoods();
        List<Goods> goodsList1 = new LinkedList<Goods>();
        if (goodsList.size() > 0) {
            for (Goods g : goodsList) {
                if (uid.equals(g.getUid()))
                    goodsList1.add(g);
            }
        }


        System.out.println(goodsList.size());
        ModelAndView mav = new ModelAndView("admin/goods/mylist");
        mav.addObject("list", goodsList1);
        return mav;
    }

    @RequestMapping("/goods/edit")
    public String goodsEdit(String id, Model model) {
        Goods goods = goodsService.getGoodsById(Integer.parseInt(id));
        model.addAttribute("model", goods);
        return "admin/goods/edit";
    }

    @RequestMapping(value = "/goods/update", method = RequestMethod.POST)
    public String goodsUpdate(String uid,
                              String code,
                              String product_name,
                              String description,
                              String thumb,
                              String content,
                              String category_name,
                              String click,
                              String category,
                              String store,
                              String vpath,
                              String price,
                              String created,
                              String updated,
                              String status,
                              int id,
                              Model model) {

        Goods modelX = goodsService.getGoodsById(id);

        modelX.setProduct_name(product_name);
        modelX.setDescription(description);
        modelX.setThumb(thumb);
        modelX.setContent(content);
        modelX.setPrice(price);

        goodsService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "操作成功");
        return "admin/goods/edit";
    }
}
